﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TodayWhoModel
{
    public class PageviewModel
    {
        private int userId;

        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        private int dayCount;

        public int DayCount
        {
            get { return dayCount; }
            set { dayCount = value; }
        }
        private DateTime statsDay;

        public DateTime StatsDay
        {
            get { return statsDay; }
            set { statsDay = value; }
        }

        public bool UpdatePageview()
        {
            string ssql = @"DECLARE @exist int;
                            DECLARE @today datetime;

                            SET @today = (SELECT CONVERT(VARCHAR(10),DATEADD(hour,9,GETDATE()),111))

                            SET @exist = (
			                            SELECT COUNT(*) FROM [PageviewStats] 
				                            WHERE userId=@userId AND 
					                            statsDay=@today)
                            IF @exist = 0
	                            INSERT INTO [PageviewStats] (userId,statsDay) 
		                            VALUES(@userId,@today) 
                            ELSE 
	                            UPDATE [PageviewStats] SET dayCount=dayCount+1
		                            WHERE userId=@userId AND statsDay=@today
	                            ";

            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userId", UserId);
            return DatabaseHelper.ExecuteNonQuerySqlWithoutIdentity(cmd);
        }

        public SqlDataReader RetrievePageViewToday()
        {
            string ssql = "SELECT * FROM [PageviewStats] ";
            ssql += " WHERE userId=@userId AND statsDay=@statsDay ";

            SqlCommand cmd = new SqlCommand(ssql);
            cmd.Parameters.AddWithValue("@userId", UserId);
            cmd.Parameters.AddWithValue("@statsDay", StatsDay);
            return DatabaseHelper.ExecuteSelectSql(cmd);
        }

        public SqlDataReader RetrievePageViewLastWeek()
        {
            string ssql = "SELECT * FROM [PageviewStats] ";
            ssql += " WHERE userId=@userId AND statsDay<=@today AND statsDay>=@lastweek ORDER BY statsDay";

            SqlCommand cmd = new SqlCommand(ssql);
            cmd.Parameters.AddWithValue("@userId", UserId);
            cmd.Parameters.AddWithValue("@today", StatsDay);
            cmd.Parameters.AddWithValue("@lastweek",StatsDay.AddDays(-6));
            return DatabaseHelper.ExecuteSelectSql(cmd);
        }
        public SqlDataReader RetrievePageViewLastMonth()
        {
            string ssql = "SELECT * FROM [PageviewStats] ";
            ssql += " WHERE userId=@userId AND statsDay<=@today AND statsDay>=@lastweek ORDER BY statsDay";

            SqlCommand cmd = new SqlCommand(ssql);
            cmd.Parameters.AddWithValue("@userId", UserId);
            cmd.Parameters.AddWithValue("@today", StatsDay);
            cmd.Parameters.AddWithValue("@lastweek", StatsDay.AddMonths(-1));
            return DatabaseHelper.ExecuteSelectSql(cmd);
        }
        public SqlDataReader RetrievePageViewTotal()
        {
            string ssql = "SELECT * FROM [PageviewStats] ";
            ssql += " WHERE userId=@userId ORDER BY statsDay";

            SqlCommand cmd = new SqlCommand(ssql);
            cmd.Parameters.AddWithValue("@userId", UserId);
            return DatabaseHelper.ExecuteSelectSql(cmd);
        }
    }
}
